/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following disclaimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: Searcher.java,v 1.4 2006/05/17 23:23:01 nano Exp $
*/

package net.jxta.myjxta.search;

import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.Resources;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;

/**
 *
 * 
 * @version $Id: Searcher.java,v 1.4 2006/05/17 23:23:01 nano Exp $
 *
 * @author james todd [gonzo at jxta dot org]
 */

public final class Searcher {

    /** The indicator that we want to search for groups */
    public static final int GROUP = 0;

    /** The indicator  that we want to search for peers */
    public static final int PIPE = 1;

    /** The indicator that we want to search for shares */
    public static final int SHARE = 2;
    private static final ResourceBundle STRINGS = Resources.getStrings();

    /** The Group to search in */
    private Group group = null;

    /** The MyJXTA instance for system wide defaults */
    private final MyJXTA myJxta;

    /** The Search instance we are currently running */
    private Search search = null;

    /** The listener that needs to be updated about the current search */
    private SearchListener listener = null;

    private List modifiers = null;
    private String label = null;

    /**
     * Create a new search
     * 
     * @param context the search context we want to search in,
     *                 i.e {@link  #GROUP GROUP},{@link  #PIPE PIPE}
     *                  or  {@link  #SHARES  SHARES}
     * @param group the group in which to search
     * @param myJxta the MyJXTA context. This group may be different
     *               from the currently selected group in myJxta
     */
    public Searcher(int context, Group group, MyJXTA myJxta) {
        this(context, group, myJxta, null);
    }


    /**
     * Create a new search
     * 
     * @param context the search context we want to search in,
     *                 i.e {@link  #GROUP GROUP},{@link  #PIPE PIPE}
     *                  or  {@link  #SHARES  SHARES}
     * @param group the group in which to search
     * @param myJxta the MyJXTA context. This group may be different
     *               from the currently selected group in myJxta
     * @param the listener that needs to keep abreast of the current
     *            search results
     */
    public Searcher(int context, Group group, MyJXTA myJxta,
                    SearchListener listener) {
        this.group = group;
        this.myJxta = myJxta;
        this.search = getSearch(context);
        this.listener = listener;
        this.modifiers = getModifiers(context);
        this.label = getLabel(context);

        if (this.listener != null) {
            addListener(this.listener);
        }
    }

    public String getLabel() {
        return this.label;
    }

    public List getModifiers() {
        return this.modifiers != null ?
               this.modifiers : Collections.EMPTY_LIST;
    }

    /**
     * Add a new SearchListener
     *
     * @param listener the listener to add
     */
    public void addListener(SearchListener listener) {
        this.listener = listener;
    }

    /**
     * Remove a previously registerer SearchListener
     *
     * @param listener the listener to remove
     */
    public void removeListener(SearchListener listener) {
        this.search.removeListener(listener);

        listener = null;
    }

    /**
     * Start the search with the indicated search term
     *
     * @param term the search term
     */
    // xxx: should add only for first start (start/stop/start)
    public void start(String term) {
        if (this.listener != null) {
            this.search.addListener(this.listener);
        }

        this.search.search(term);
    }

    /**
     * Terminate the current search
     */
    public void stop() {
        this.search.cancel();

        // if the listener is not null, unregister it
        if (this.listener != null) {
            this.listener.terminate();
            this.search.removeListener(this.listener);
        }
    }

    /**
     * Get the Search instance suitable for the desired 
     * search
     *
     * @param context the search to perform
     */
    private Search getSearch(int context) {
        Search search = null;

        switch (context) {
        case GROUP:
            search = new GroupSearch(this.group, myJxta);

            break;
        case PIPE:
            search = new PipeSearch(this.group, myJxta);

            break;
        case SHARE:
            search = new ShareSearch(this.group, myJxta);

            break;
        }

        return search;
    }

    private String getLabel(int context) {
        String l = null;

        switch (context) {
        case GROUP:
            l = STRINGS.getString("label.group.name");

            break;
        case PIPE:
            l = STRINGS.getString("label.pipe.name");

            break;
        case SHARE:
            l = STRINGS.getString("label.share.name");

            break;
        }

        return l;
    }

    private List getModifiers(int context) {
        List<SearchModifier> m = new ArrayList<SearchModifier>();

        switch (context) {
        case GROUP:
            m.add(new SearchModifier(SearchModifier.EQUALITY));
            m.add(new SearchModifier(STRINGS.getString("label.search.modifier.contains"),
                                     SearchModifier.WILDCARD,
                                     SearchModifier.WILDCARD));
            m.add(new SearchModifier(SearchModifier.DEFAULT, "",
                                     SearchModifier.WILDCARD));
            m.add(new SearchModifier(STRINGS.getString("label.search.modifier.ends"),
                                     SearchModifier.WILDCARD, ""));

            break;
        case PIPE:
            m.add(new SearchModifier(SearchModifier.EQUALITY));
            m.add(new SearchModifier(SearchModifier.DEFAULT, "",
                                     SearchModifier.WILDCARD));
            break;
        case SHARE:
            m.add(new SearchModifier(SearchModifier.EQUALITY));
            m.add(new SearchModifier(STRINGS.getString("label.search.modifier.contains"),
                                     SearchModifier.WILDCARD,
                                     SearchModifier.WILDCARD));
            m.add(new SearchModifier(SearchModifier.DEFAULT, "",
                                     SearchModifier.WILDCARD));
            m.add(new SearchModifier(STRINGS.getString("label.search.modifier.ends"),
                                     SearchModifier.WILDCARD, ""));

            break;
        }

        return m;
    }
}
